#!/bin/bash

killall daemon
killall tuntap_bb
sleep 3

DEVICE_ID="1d6b:8030"

if lsusb | grep -q "$DEVICE_ID"; then
    echo "设备 $DEVICE_ID 存在，初始化401"

    ln -s   /dev/net/tun  /dev/tun
    echo "初始化linux的tun"
    sleep 1
    ./bin/daemon &
    echo "运行daemon"
    sleep 8

    echo "升级401，dev版本的固件"
    ./dev_helper/ota -f firmware_24G/VT4_DEV-USBBOX_V1.0.0_24dub.img
    sleep 10
    echo "恢复出场设置"
    ./bin/bb_test  -R
    sleep 3
    echo "重启设备"
    ./bin/bb_test -o
    sleep 3
    echo "设置role为dev:"
    ./bin/bb_test -r 1
    echo "开始配对"
    ./bin/bw_update_demo -s 0 -r -t 100 -d -A -j
     sleep 3

    echo "配对成功后，创建虚拟网卡"
  
    
    ./dev_helper/tuntap_bb -p 2 -i 192.168.144.66  -u 0 -d tun -r 40000 -t 60000 &
    sleep 3

    echo "成功，尝试ping 192.168.144.55"



    TARGET_IP="192.168.144.55"

    if ping -c 1 -W 1 $TARGET_IP &> /dev/null; then
        echo "成功: $TARGET_IP 可达"
    else
        echo "失败: $TARGET_IP 不可达"
    fi



    exit 0
else
    echo "设备 $DEVICE_ID 不存在,请检查401设备是否正常"
    exit 1
fi
